import Vue from 'vue'
import Vuex from 'vuex'
import { Request } from '../common/config.js'
import {checkOpenGPSServiceByAndroid } from '../common/openSettings.js'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        hasLogin: uni.getStorageSync('uid')?true:false,
        userinfo: {},
        ppid: '',
		userid:uni.getStorageSync('uid') || '',
		sex:uni.getStorageSync('sex') || '',
		tabbarlist:[{
				iconPath: "/static/navs/home.png",
				selectedIconPath: "/static/navs/home-fill.png",
				text: '主页',
				customIcon: false,
				pagePath: "/pages/index/index"
			},
			{
				iconPath: "/static/navs/moments.png",
				selectedIconPath: "/static/navs/moments-fill.png",
				text: '动态',
				customIcon: false,
				pagePath: "/pages/dymanic/index"
			},
			{
				iconPath: "/static/navs/jia.png",
				selectedIconPath: "/static/navs/jia-fill.png",
				text: '',
				customIcon: false,
				pagePath: "pages/pubdymanic/index"
			},
			{
				iconPath: "/static/navs/chat.png",
				selectedIconPath: "/static/navs/chat-fill.png",
				text: '消息',
				count: 0,
				// isDot: true,
				customIcon: false,
				pagePath: "/pages/msgcenter/index"
			},
			{
				iconPath: "/static/navs/account.png",
				selectedIconPath: "/static/navs/account-fill.png",
				text: '我的',
				customIcon: false,
				pagePath: "/pages/ucenter/index"
			},
		],
		tabbarcurrent: 0,
		longitude:0,
		latitude:0,
		themeObj: {},
		isVip: 0,
		rz: 0,
		openid: ''
    },
    mutations: {
        // 登录
        login(state,res) {
            state.hasLogin = true
            state.userinfo = res
            state.sex = res.sex
            state.isVip = res.is_vip
			state.userid = res.id
            state.rz = res.is_identity_authentication
            uni.setStorage({key: 'uid',data: res.id})
            uni.setStorage({key: 'sex',data: res.sex})
            uni.setStorage({key: 'isVip',data: res.is_vip})
            uni.setStorage({key: 'rz',data: res.is_identity_authentication})
            // console.log(state.userinfo)
            // console.log(state.hasLogin)
        },
		position(state,res){
			state.longitude = res.longitude
			state.latitude = res.latitude
		},
        // 二维码ppid
        saveppid(state,ppid) {
            state.ppid = ppid
            uni.setStorage({
                key: 'ppid',
                data: ppid
            })
            // console.log(state.ppid)
        },
		setopenid(state,openid){
			state.openid = openid
			uni.setStorageSync("openid",openid)
		},
		logout(state){
			state.userid = ''
			state.sex = ''
			uni.removeStorage({key:'sex'})
			uni.removeStorage({key:'uid'})
			uni.redirectTo({
				url:'/pages/appview/index'
			})
		},
		settheme(state, obj){
			state.themeObj = obj
		},
		msgNum(state, obj){
			state.tabbarlist[2].count = obj.cnum + obj.mnum
		}
    },
	actions:{
		getUserPosition({commit , rootState}){
			console.log('地图走这里了吗2');
			return new Promise(async resolve =>{
				/*#ifdef APP*/				
				uni.getLocation({
					type: 'wgs84',
					success: res => {
						commit('position',res)
						resolve(res)
						uni.setStorage({key: 'longitude',data: res.longitude})
						uni.setStorage({key: 'latitude',data: res.latitude})
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
					}, 
					fail: error => {
						uni.showModal({
							title: '提示',
							content: '需要开启定位权限才能获取当前位置，请前往设置中心开启。',
							confirmText: '去开启',
							showCancel:true,
							success: function(res) {
							  if (res.confirm) {
								// 用户点击确认，跳转到设置页面
								checkOpenGPSServiceByAndroid()
							  } else {
							  }
							}
						});
					}
				});
				/*#endif*/
				/*#ifndef APP*/
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.getLocation({
							type: 'wgs84',
							success: function (res) {
								commit('position',res)
								resolve(res)
								uni.setStorage({key: 'longitude',data: res.longitude})
								uni.setStorage({key: 'latitude',data: res.latitude})
								console.log('当前位置的经度：' + res.longitude);
								console.log('当前位置的纬度：' + res.latitude);
							},
							fail(e) {
								console.log('我看看地图什么错误');
								console.log(e);
							}
						});
					},
					fail(err) {
						// uni.showModal({
						// 	title: '提示',
						// 	content: '需要开启定位权限才能获取当前位置，请前往设置中心开启。',
						// 	confirmText: '去开启',
						// 	showCancel:false,
						// 	success: function(res) {
						// 	  if (res.confirm) {
						// 		// 用户点击确认，跳转到设置页面
						// 		checkOpenGPSServiceByAndroid()
						// 	  } else {
						// 	  }
						// 	}
						// });
					}
				})
				/*#endif*/
			})
		},
		getuserinfo({commit , rootState}){
			return new Promise(async resolve =>{
				const { data: res } = await Request({
					url:'api/user/info',
					data:{
						user_id: rootState.userid,
						longitude: rootState.longitude,
						latitude: rootState.latitude
					}
				})
				
				if(res.code === 200){
					if (res.result.is_banned == 1) {
						uni.showModal({
							title:'该账号已被冻结，解封时间为' + res.result.banned_end_time + ', 如有疑问，请联系客服',
							showCancel:false,
							success(res) {
								if(res.confirm){
									commit('logout')
								}
							}
						})
						return;
					}
					commit('login',res.result)
					resolve(res.result)
				}
			})
		},
		// getopenid({commit, rootState}){
		// 	console.log('难道是这里跳转微信？');
		// 	return new Promise(resolve =>{
		// 		uni.login({
		// 		  provider: 'weixin',
		// 		  async success(loginRes) {
		// 			const {data: res} = await Request({
		// 				url: 'api/login/wxlogin',
		// 				data: {
		// 					code: loginRes.code
		// 				}
		// 			})
		// 			commit('setopenid',res.openid)
		// 			resolve(res.openid)
		// 		  }
		// 		});
		// 	})
		// },
		getopenid1({commit, rootState}){
			console.log('执行了么'); 
			return new Promise(resolve =>{
				uni.login({
				  provider: 'weixin',
				  async success(loginRes) {
					const {data: res} = await Request({
						url: 'api/login/wxlogin',
						data: {
							code: loginRes.code
						}
					})
					commit('setopenid',res.openid)
					resolve(res.openid)
				  }
				});
			})
		},
		setOnline({rootState}, state){
			if(rootState.userid){
				Request({
					url:'api/login/setOnline',
					data:{
						state: state,
						user_id: rootState.userid
					}
				})
			}
		},
		async getcate({commit , rootState}) {
			const {
				data: res
			} = await Request({
				url: 'apis/home/dymanic/category'
			})
			var obj = []
			for (var i = 0, len = res.result.length; i < len; i++) {
				obj[res.result[i].id] = res.result[i].name;
			}
			commit('settheme',obj)
		},
	}
})

export default store